import Vue from 'vue';
import {mapGetters,mapActions} from 'vuex';
function request(url, method, data, resbook){
    return new Promise((resolve, reject)=>{
        uni.showLoading({
            title: '加载中...'
        });
        uni.request({
            url: url,
            data: data || {},
            method: method || 'GET',
            success: function(res) {
				//console.log(res)
                uni.hideLoading();
                if(res.data.code==200 || res.data.code==202){
                    resolve(res.data);
                }else{
                    var title = '';
					if(res.data.code==205){  //session_token过期
					    title = '请先登陆再查看';
					}else{
						
					};
					showToast(title);  //提示信息
					
					if(res.data.code==205){
						removeLocalStorage('userInfo');
						Vue.prototype.$store.dispatch('setUserInfo','');
						setTimeout(()=>{
						    uni.switchTab({
						        url:'/pages/me/main',
						        success(res){ 
						            /* let page = getCurrentPages().pop();
						            if (page == undefined || page == null) { 
						                return;
						            } 
						            page.onLoad(); */
						        } 
						    });
						},1500);
					}else{
						reject();
					};
                } 
            }
        });
    })
};

//toast提示信息
export function showToast(title, icon='none'){
    uni.showToast({
        title,
        icon
    });
};

export function get(url, data){
    return request(url, 'GET', data);
};

export function post(url, data){
    return request(url, 'POST', data);
};


//根据key获取storage
export function getLocalStorage(key){
    return uni.getStorageSync(key);
};

//根据key删除storage
export function removeLocalStorage(key){
    return uni.removeStorageSync(key);
};

//根据key value设置storage
export function setLocalStorage(key,value){
    uni.setStorageSync(key,value);
};

//将日期小于10的转为两位数，  9-> 09
export function numberDouble(num){
    return num<10? '0'+num: num;
};

//将时间戳转为日期时间
export function forMatDate(date,default_val=true){  //中国标准时间对象
    var year=date.getFullYear();
    var month=numberDouble(date.getMonth()+1);
    var dat=numberDouble(date.getDate());
    var hours=numberDouble(date.getHours());
    var min=numberDouble(date.getMinutes());
    var sen=numberDouble(date.getSeconds());
    if(default_val){
        date=year+'-'+month+'-'+dat+' '+hours+':'+min+':'+sen;
    }else{
        date=year+month+dat+hours+min+sen;
    }
    return date;
};


//获取系统信息
export function getSystemInfo(){
	return uni.getSystemInfoSync();
};


//设置剪贴板数据
export function setClipboardData(data){
	uni.setClipboardData({
		data: data,
		success(){
			showToast('复制成功','success');
		},
		fail(){
			showToast('复制失败');
		}
	})
};


//获取剪切板内容
export function getClipboardData(){
	return new Promise((resolve,reject)=>{
		uni.getClipboardData({
			success(res){
				if(res.data){
					resolve(res.data);
				}else{
					showToast('剪贴板无内容');
					reject();
				}
			},
			fail(){
				showToast('获取失败');
				reject();
			}
		});
	});
}


//正则匹配电话(包括手机号和电话)
export function checkPhone(tele){
	var reg1 = /^1\d{10}$/g;  //匹配手机
	var reg2 = /^0\d{2,3}-?\d{7,8}$/g;  //匹配电话
	return (reg1.test(tele) || reg2.test(tele));
}

//检测是否包含html标签
export function checkHtml(str){
	var reg = reg=/<\/?.+?\/?>/g;
	return reg.test(str);
}